<?php

declare(strict_types=1);

namespace App\Services;

use Hyperf\Di\Annotation\Inject;
use App\Exception\ApiException;
use App\Constants\ErrorCode;
use App\Services\ActivityService;
use App\Model\Banner;
use App\Model\Goods;
use App\Model\GoodsCategory;
use App\Model\News;
use App\Model\User;
use App\Model\Activity;
use App\Model\PointsRanking;

class IndexService extends BaseService
{
    /**
     * @Inject()
     * @var ActivityService
     */
    protected $activityService;

    public function getBanner()
    {
        $banner = Banner::query()->where('type', 0)->where('status', 1)->where('deleted', 0)->get();

        return $banner;
    }

    public function getBgImage()
    {
        $bgImage = Banner::query()->where('type', 1)->where('status', 1)->where('deleted', 0)->orderBy('id', 'DESC')->first();

        return $bgImage;
    }

    public function getNews()
    {
        $topNews = News::query()->with('newsCategory')->select('id','title','category_id','created_at')->where('is_top', 1)->where('status', 1)->where('deleted', 0)->orderBy('id', 'DESC')->first();

        $news = News::query()->with('newsCategory')->select('id','title','category_id','created_at')->where('is_top', 0)->where('status', 1)->where('deleted', 0)->limit(6)->orderBy('id', 'DESC')->get()->toArray();
        foreach($news as $key=>$val)
        {
            $news[$key]['date'] = date('m-d', strtotime($val['created_at']));
        }

        return ['top'=>$topNews, 'list'=>$news];
    }

    public function getActivity()
    {
        $activity = Activity::query()->select('id','title','image','begin_time','end_time','is_new')->where('status', 1)->where('deleted', 0)->orderBy('begin_time', 'DESC')->limit(4)->get()->toArray();
        foreach($activity as $key=>$val)
        {
            $activity[$key]['status'] = $this->activityService->checkTimeStatus($val['begin_time'], $val['end_time']);
        }

        return $activity;
    }

    public function getMonthPointsRanking()
    {
        $lastMonthYear = get_last_month_year();
        $year = $lastMonthYear['year'];
        $month = $lastMonthYear['month'];

        //临时数据处理
        // $pointsRanking = PointsRanking::query()->where('year', $year)->where('month', $month)->orderBy('points', 'DESC')->limit(10)->get()->toArray();
        // foreach($pointsRanking as $key=>$val)
        // {
        //     $pointsRanking[$key]['user']['nickname'] = $val['nickname'];
        //     $pointsRanking[$key]['user']['avatar'] = 'https://admin.pubgjiscar.com/upload/41/418333e4c28692716dbdadac38dabd.png';
        //     $rank = $key+1;
        //     $pointsRanking[$key]['rank_icon'] = env('HTTP_HOST').'/public/images/rank/'.$rank.'.png';
        //     $pointsRanking[$key]['level_icon'] = env('HTTP_HOST').'/public/images/rank/'.$val['level'].'.png';
        // }

        $pointsRanking = PointsRanking::query()->with('user')->where('year', $year)->where('month', $month)->where('deleted', 0)->orderBy('points', 'DESC')->limit(10)->get()->toArray();
        foreach($pointsRanking as $key=>$val)
        {
            $rank = $key+1;
            $pointsRanking[$key]['rank_icon'] = env('HTTP_HOST').'/public/images/rank/'.$rank.'.png';
            $pointsRanking[$key]['level_icon'] = env('HTTP_HOST').'/public/images/rank/'.$val['user']['level'].'.png';
        }

        return ['month' => intval($month), 'points_ranking' => $pointsRanking];
    }

    public function getGoods(array $params)
    {
        $goods = Goods::query()->select('id','category_id','title','image','price','num')->where('category_id', $params['category_id'])->where('status', 1)->where('deleted', 0)->limit(6)->get()->toArray();

        return $goods;
    }

}